<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      "use strict";
      function User(name, age) {
        let data={name,age}
        Object.defineProperties(this,{
            name:{
                get(){
                    return data.name;
                },
                set(value){
                    if(value.trim()==""||value.length>20){
                        throw new Error('用户名不合法')
                    }
                    data.name=value
                }
            },
            age:{
                get(){
                    return data.age;
                },
                set(value){
                    data.age=value
                }
            }
        })
      }
      let xh = new User("小灰", 19);
      xh.name='刘辉';
      console.log(xh);
    </script>
  </body>
</html>
